﻿using PrismApp.Views;
using Prism.Ioc;
using Prism.Modularity;
using System.Windows;
using Prism.Unity;
using Prism.Mvvm;
using System;
using PrismApp.ViewModels;
using System.Windows.Controls;
using PrismApp.Interface;
using PrismApp.Service;

namespace PrismApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        /// 启动的界面
        /// </summary>
        /// <returns></returns>
        protected override Window CreateShell()
        {
            return Container.Resolve<CommandWindow>();
        }

        /// <summary>
        /// 给容器注册类型
        /// </summary>
        /// <param name="containerRegistry"></param>
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<IOrder, OderService>();
        }

        /// <summary>
        /// 配置ViewModel
        /// </summary>
        protected override void ConfigureViewModelLocator()
        {
            //base.ConfigureViewModelLocator();
            //第一种：自定义ViewModel文件后缀，自动匹配
            //prism:ContainerProvider Type="{x:Type vm:FirstWindowVM}"优先级最高
            //ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
            //{
            //    var viewName = viewType.FullName.Replace("Views", "ViewModels");
            //    var viewAssemblyName = viewType.Assembly.FullName;
            //    var viewModelName = $"{viewName}ViewModel,{viewAssemblyName}";

            //    return Type.GetType(viewModelName);
            //});

            // 第二种：一对一注册
            //prism:ViewModelLocator.AutoWireViewModel = "True" //这个属性不必要
            //1、写法1
            //ViewModelLocationProvider.Register(typeof(FirstWindow).ToString(), typeof(FirstWindowVM));
            //2、写法2
            //ViewModelLocationProvider.Register(typeof(FirstWindow).ToString(),
            //    () =>
            //    Container.Resolve<FirstWindowVM>()
            //);
            //3、写法3
            //ViewModelLocationProvider.Register<FirstWindow, FirstWindowVM>();
        }
    }
}
